package com.atguigu.school.service.impl;

import com.atguigu.school.commons.Page;
import com.atguigu.school.mapper.GradeMapper;
import com.atguigu.school.pojo.Grade;
import com.atguigu.school.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {

    /**
     * 访问年级数据
     */
    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public void saveOrUpdateGrade(Grade grade) {
        if (grade.getId() == null || grade.getId() == 0) {
            this.gradeMapper.insertGrade(grade);
        } else {
            this.gradeMapper.updateGradeById(grade);
        }
    }

    @Override
    public void deleteGradesByIds(List<Integer> ids) {
        this.gradeMapper.deleteGradesByIds(ids);
    }

    @Override
    public Page<Grade> selectGradesForPage(Integer current, Integer size, String name) {
        Integer total = this.gradeMapper.countGradesByName(name);
        Page<Grade> page = new Page<>(current, size, total);

        List<Grade> grades = this.gradeMapper.selectGradesForPage(page.getOffset(), size, name);
        page.setRecords(grades);
        return page;
    }

    @Override
    public List<Grade> selectGrades() {
        return this.gradeMapper.selectGrades();
    }
}
